package com.lqr.wechat.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.lqr.wechat.R;
import com.lqr.wechat.databinding.ActivityRedPacketBinding;
import com.lqr.wechat.utils.UIUtils;

/**
 * @创建者 CSDN_LQR
 * @描述 红包
 */
public class RedPacketActivity extends BaseActivity {
    private ActivityRedPacketBinding binding ;
    @Override
    protected int layoutId() {
        return R.layout.activity_red_packet;
    }

    @Override
    public void initView() {
        binding = getBind() ;
        initToolbar();
    }

    @Override
    public void initListener() {
        binding.etMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    binding.btnOk.setEnabled(false);
                    binding.tvMoney.setText("￥0.00");
                } else {
                    binding.btnOk.setEnabled(true);
                    String result = String.format("%.2f", Double.valueOf(s.toString()));
                    binding.tvMoney.setText("￥" + result);
                    Double money = Double.valueOf(result);
                    if (money > 200) {
                        binding.tvTip.setVisibility(View.VISIBLE);
                        binding.tvMoneyLeft.setTextColor(UIUtils.getColor(R.color.red5));
                        binding.etMoney.setTextColor(UIUtils.getColor(R.color.red5));
                        binding.tvMoneyRight.setTextColor(UIUtils.getColor(R.color.red5));
                        binding.btnOk.setEnabled(false);
                    } else {
                        binding.tvTip.setVisibility(View.GONE);
                        binding.tvMoneyLeft.setTextColor(UIUtils.getColor(R.color.black0));
                        binding.etMoney.setTextColor(UIUtils.getColor(R.color.black0));
                        binding.tvMoneyRight.setTextColor(UIUtils.getColor(R.color.black0));
                        binding.btnOk.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.etMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.tvHint.setVisibility(TextUtils.isEmpty(s) ? View.VISIBLE : View.GONE);
                if (s.length() > 25) {
                    binding.etMessage.setText(s.subSequence(0, 25));
                    binding.etMessage.setSelection(25);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu_one_icon, menu);
        menu.getItem(0).setIcon(R.mipmap.ic_question);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
        } else if (itemId == R.id.itemOne) {
            UIUtils.showToast("有问题？");
        }
        return super.onOptionsItemSelected(item);
    }

    private void initToolbar() {
        setSupportActionBar(binding.lyToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("发红包");
        getSupportActionBar().setSubtitle("微信安全支付");
        binding.lyToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        binding.lyToolbar.toolbar.setBackgroundColor(UIUtils.getColor(R.color.red1));
    }

}
